/**
 * filter.js
 * 筛选器reducer
 *
 * created by xurui
 * version 1.0.0
 * 2016年03月31日
 */

import { CITY, COUNTY, SCHOOL } from '../../../constants/basicOverview';
import moment from 'moment';
import { DATE_FORMMATTER } from '../../../constants/datetime';
import { merge } from 'lodash/object';
import { cloneDeep }from 'lodash/lang';
import {
	GET_CITY_OPTIONS,
	SELECTED_CITY,
	SELECTED_COUNTY,
	GET_COUNTY_OPTIONS_REQUEST,
	GET_COUNTY_OPTIONS_SUCCESS,
	GET_COUNTY_OPTIONS_FAILURE,
	SELECTED_SCHOOL,
	GET_SCHOOL_OPTIONS_REQUEST,
	GET_SCHOOL_OPTIONS_SUCCESS,
	GET_SCHOOL_OPTIONS_FAILURE,
	GET_SUBJECT_REQUEST,
	GET_SUBJECT_SUCCESS,
	GET_SUBJECT_FAILURE,
	SELECTED_SUBJECT,
	GET_GRADE_REQUEST,
	GET_GRADE_SUCCESS,
	GET_GRADE_FAILURE,
	SELECTED_GRADE,
	SET_DATE_RANGE,
	RESET
} from '../actions/filter';

let now = moment().subtract(1, 'days');
let lastMonth = moment().subtract(1, 'M');

/**
 * 初始基础概况页面状态
 * @type {Object}
 */
export const INITIALIZE_STATE = {
	// 筛选条件
	// 地域 - 城市
	city: {
		// { enum: <id>, name: <name> }
		options: [],
		selectedItem: {}
	},
	// 地域 - 区县
	county: {
		isFetch: false,
		options: [],
		selectedItem: {}
	},
	// 地域 - 学校
	school: {
		isFetch: false,
		options: [],
		selectedItem: {}
	},
	// 学科
	subject: {
		isFetch: false,
		options: [],
		selectedItems: []
	},
	// 年级
	grade: {
		isFetch: false,
		options: [],
		selectedItems: []
	},
	// 日期范围
	dateRange: [lastMonth.format(DATE_FORMMATTER), now.format(DATE_FORMMATTER)],
	
};

/**
 * 处理基础
 * @param  {[type]} state  [description]
 * @param  {Object} action [description]
 * @return {[type]}        [description]
 */
export default function filter(state = INITIALIZE_STATE, action = {}) {
	let { type,...others } = action;

	switch(type) {
		case GET_CITY_OPTIONS:
			{
				let { options } = others.payload;
				let city = cloneDeep(state.city);
					city = Object.assign({},city,{
					options
				})
				let newState = Object.assign({},state,{
					city
				});
				return newState;
			}
		case SELECTED_CITY:
			// TODO: 选择地域，获取用户选择城市列表
			{	
				let { selectedItem } = others.payload;
				let city = cloneDeep(state.city);
					city = Object.assign({},city,{
					selectedItem
				})
				let newState = Object.assign({},state,{
					city
				});
				return newState;
			};
		
		case SELECTED_COUNTY:
			// TODO: 选择地域，获取用户选择区县列表
			{
				let { selectedItem } = others.payload;
				let county = cloneDeep(state.county);
					county = Object.assign({},county,{
					selectedItem
				})
				let newState = Object.assign({},state,{
					county
				});
				return newState;
			};
		case GET_COUNTY_OPTIONS_REQUEST:
			// TODO: 选择地域，请求区县列表
			{ 
				let newState = merge({},state,{
					country:{
						isFetch:true
					}
				});
				return newState;
			};
		case GET_COUNTY_OPTIONS_SUCCESS:
			// TODO: 选择地域，获取区县列表成功
			{ 
				let { options } = others.payload;

				let county=cloneDeep(state.county);
					county.options=options;
					county.isFetch=false;
				let newState = Object.assign({},state,{
					county
				});
				return newState;
			};
		case GET_COUNTY_OPTIONS_FAILURE:
			// TODO: 选择地域，获取区县列表失败
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					country:{
						isFetch:false,
						errorMessage
					}
				});
				return newState;
			};
		case SELECTED_SCHOOL:
			// TODO: 选择地域，用户选择学校列表
			{
				let { selectedItem } = others.payload;
				let school = cloneDeep(state.school);
					school = Object.assign({},school,{
					selectedItem
				})
				let newState = Object.assign({},state,{
					school
				});
			
				return newState;
			};
		case GET_SCHOOL_OPTIONS_REQUEST:
			// TODO: 选择地域，请求学校列表
			{
				let newState = merge({},state,{
					school:{
						isFetch:true
					}
				});
				return newState;
			};
		case GET_SCHOOL_OPTIONS_SUCCESS:
			// TODO: 选择地域，获取学校列表成功
			{
				let { options } = others.payload;
				let school=cloneDeep(state.school);
					school.options=options;
					school.isFetch=false;
				let newState = Object.assign({},state,{
					school
				});
				return newState;
			};
		case GET_SCHOOL_OPTIONS_FAILURE:
			// TODO: 选择地域，获取学校列表失败
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					school:{
						isFetch:false,
						errorMessage
					}
				});
				return newState;
			};
		case GET_SUBJECT_REQUEST:
			// TODO: 发送获取科目列表请求
			{
				let newState = merge({},state,{
					subject:{
						isFetch:true
					}
				});
				return newState;
			};
		case GET_SUBJECT_SUCCESS:
			// TODO: 获取科目列表成功，更新state
			{	let { options } = others.payload;

				let subject=cloneDeep(state.subject);
					subject.options=options;
					subject.isFetch=false;
				let newState = Object.assign({},state,{
					subject
				});
				return newState;
			};
		case GET_SUBJECT_FAILURE:
			// TODO: 获取科目列表失败，获取错误消息
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					message:errorMessage,
					subject:{
						isFetch:false
						
					}
				});
				return newState;
			};
		case SELECTED_SUBJECT:
			// TODO: 获取用户选择科目列表
			{	
				let { selectedItems } = others.payload;
				let subject=cloneDeep(state.subject);
					subject.selectedItems=selectedItems;
					subject.isFetch=false;
				let newState = Object.assign({},state,{
					subject
				});
				return newState;
				
			};
		case GET_GRADE_REQUEST:
			// TODO: 异步发送年级列表请求
			{
				let newState = merge({},state,{
					grade:{
						isFetch:true
					}
				});
				return newState;
			};
		case GET_GRADE_SUCCESS:
			// TODO: 成功获取选择年级数据，停止通讯
			{	let { options } = others.payload;
			
				let grade=cloneDeep(state.grade);
					grade.options=options;
					grade.isFetch=false;
				let newState = Object.assign({},state,{
					grade
				});
				return newState;
			};
		case GET_GRADE_FAILURE:
			// TODO: 获取选择的年级列表失败，停止通讯，返回错误消息
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					message:errorMessage,
					grade:{
						isFetch:false
					}
				});
				return newState;
			};
		case SELECTED_GRADE:
			//TODO:用户选择年级列表
			{
				let { selectedItems } = others.payload;
				let grade=cloneDeep(state.grade);
					grade.selectedItems=selectedItems;
					grade.isFetch=false;
				let newState = Object.assign({},state,{
					grade
				});
				return newState;
			}
		/**
		 * 设置日期范围
		 * @type {[type]}
		 */
		case SET_DATE_RANGE:
			// TODO: 获取开始日期范围数据
			{	
				let { dateRange } = others.payload;
				let newState = Object.assign({},state,{
					dateRange
				});
			
				return newState;
			};
		/**
		 * 重置筛选器
		 * @type {[type]}
		 */
		case RESET: 
			{
				return INITIALIZE_STATE;
			}
		default: 
			return state;
	}
};